#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>

void main()
{
		int fbdev;
		int *fb;
		struct fb_var_screeninfo vinfo;
		struct fb_fix_screeninfo finfo;
		long int screensize = 0;
		int i,j,k;

		fbdev=open("/dev/fb0", O_RDWR);

    if (fbdev == -1) {
        perror("Error: cannot open framebuffer device");
        exit(1);
    }
    printf("The framebuffer device was opened successfully.\n");

    // Get fixed screen information
    if (ioctl(fbdev, FBIOGET_FSCREENINFO, &finfo) == -1) {
        perror("Error reading fixed information");
        exit(2);
    }

    // Get variable screen information
    if (ioctl(fbdev, FBIOGET_VSCREENINFO, &vinfo) == -1) {
        perror("Error reading variable information");
        exit(3);
    }

    printf("vinfo.xres=%d,vinfo.yres=%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);

    // Figure out the size of the screen in bytes
#if 1
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
#else
		screensize = 1360*768*4;
#endif

    fb=(short *)mmap(0, screensize, PROT_WRITE, MAP_SHARED, fbdev, 0);

    if ((int)fb == -1) {
        perror("Error: failed to map framebuffer device to memory");
        exit(4);
    }
    printf("The framebuffer device was mapped to memory successfully.\n");

    fprintf(stderr, "fb opened at %x, fd %d\n", fb, fbdev);
		
		//char fb_array[1360][768][4];
		for(i=0;i<vinfo.yres;i++)//vinfo.yres*2-300
		{
#if 1
				for(j=0;j<vinfo.xres;j++)
#else
				for(j=0;j<1360;j++)
#endif
				{
						//0x00FF0000 红
						//0x0000FF00 绿
						//0x000000FF 蓝
						fb[i*vinfo.xres + j] = 0x55FF0000;//alpha,red,green,blue
				}
		}

		munmap(fb, screensize);
		close(fbdev);
}
